import React, { createContext, useState } from 'react';  
  
// 创建一个 Context 对象  
const UploadContext = createContext();  
  
// 创建一个 Provider 组件来包裹应用或需要共享状态的部分  
const UploadProvider = ({ children }) => {  
    const [isUploading, setIsUploading] = useState(false);  
    const [userInfo, setUserInfo] = useState(null); // 新增用户信息状态  
  
    // 提供 startUpload 和 endUpload 方法来改变上传状态  
    const startUpload = () => {  
        setIsUploading(true);  
        console.log(isUploading);
    };  
  
    const endUpload = () => {  
        setIsUploading(true);  
        console.log(isUploading);
    };  

    // 提供设置用户信息的方法  
    const setUser = (user) => {  
        setUserInfo(user);  
    };  

    const logout = () => {  
        setUserInfo(null); // 清除用户信息  
        console.log(userInfo);
        localStorage.removeItem('userInfo');
        console.log(localStorage.getItem('userInfo'))
        // 这里可以添加其他登出逻辑，比如清除localStorage中的token等  
    }; 
  
    // 使用 Provider 组件包裹子组件，并传递 value 属性  
    return (  
        <UploadContext.Provider value={{ isUploading, startUpload, endUpload, userInfo, setUser, logout}}>  
            {children}  
        </UploadContext.Provider>  
    );  
};  
  
export { UploadContext, UploadProvider };